package com.lcu.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcu.reggie.common.CustomException;
import com.lcu.reggie.dto.SetmealDto;
import com.lcu.reggie.entity.Setmeal;
import com.lcu.reggie.entity.SetmealDish;
import com.lcu.reggie.mapper.SetmealMapper;
import com.lcu.reggie.service.SetmealDishService;
import com.lcu.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 重写新增套餐方法
     * @param setmealDto
     */
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //将套餐基本数据保存
        this.save(setmealDto);
        Long setmealId = setmealDto.getId();
        //保存套餐内的菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 重写修改的方法
     * @param setmealDto
     */
    @Transactional
    public void updateWhitDish(SetmealDto setmealDto) {
        //修改套餐基本信息
        this.updateById(setmealDto);

        //先将之前的菜品信息删除
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //修改菜品信息
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        //将SetmealDish中的setmealId赋值
        list=list.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //然后再重新保存信息
        setmealDishService.saveBatch(list);
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        //构建条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.in(Setmeal::getId,ids);
        queryWrapper1.eq(Setmeal::getStatus,1);
        //查询有多少个正在起售
        int count=this.count(queryWrapper1);

        if (count>0){
            //抛出一个异常
            throw new CustomException("存在套餐售卖，不可删除");
        }
        //删除基本信息
        this.removeByIds(ids);
        //构建删除条件
        LambdaQueryWrapper<SetmealDish> queryWrapper2=new LambdaQueryWrapper<>();
        queryWrapper2.in(SetmealDish::getSetmealId,ids);
        //删除菜品信息
        setmealDishService.remove(queryWrapper2);

    }
}
